<?php
namespace Controller;

use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Container;
use Intervention\Image\ImageManagerStatic as Image;


class ClothingApi extends Base
{

    public function __construct(Container $container)
    {
        parent::__construct($container);

        if ($this->getaccount() === false) {
            header("HTTP/1.1 400 Bad Request");
            echo 'no account error';
            exit;
        }else{
            $this->account=$this->getaccount();
        }

    }


    public function createclothing(Request $req, Response $res)
    {
        $validate = $this->guard->validateToken($req->getParam('tk_name'), $req->getParam('tk_value'));
        if ($validate === true) {
            if ($UploadedFiles = $req->getUploadedFiles()) {
                $file = $UploadedFiles['media'];
                $Size = $file->getSize();
                $name = explode('.', $file->getClientFilename());
                $checksize = $this->checksize($Size, 10);
                if ($checksize === false) {
                    return $res->withStatus(400)->write('文件过大');
                    exit;
                }

                $filetype = ['jpg', 'png', 'jpeg', 'JPG', 'PNG', 'image/jpeg', 'image/png'];
                if (in_array($name[1], $filetype) === false) {
                    return $res->withStatus(400)->write('文件类型错误');
                    exit;
                }
                $filepath = $this->checkuploadfile();
                $newname = uniqid() . '.' . $name[1];
                $movefile = $filepath['filepath'] . '/' . $newname;
                $movethumbfile = $filepath['thumbfilepath'] . '/' . $newname;
                Image::make($file->file)->resize(300,200)->save($movethumbfile);
                $file->moveTo($movefile);
            } else {
                $newname = '';
            }


            $id = $req->getParam('cid');
            $clothing_name = $req->getParam('clothing_name');
            $minage = $req->getParam('minage');
            $maxage = $req->getParam('maxage');
            $starttime = $req->getParam('starttime');
            $endtime = $req->getParam('endtime');
            $integral = $req->getParam('integral');
            $num = $req->getParam('num');
            $rank = $req->getParam('rank');
            $tkcode = $req->getParam('tkcode');
            $content = $req->getParam('content');

            if ($this->checkfrom->regex('require', $clothing_name) === false) {
                return $res->withStatus(400)->write('服装名称不能为空');
                exit;
            }
            if ($this->checkfrom->regex('number', $minage) === false || $this->checkfrom->regex('number', $maxage) === false) {
                return $res->withStatus(400)->write('适合年龄错误');
                exit;
            }
            if ($this->checkfrom->regex('require', $starttime) === false || $this->checkfrom->regex('require', $endtime) === false) {
                return $res->withStatus(400)->write('活动时间错误');
                exit;
            }
            if ($this->checkfrom->regex('number', $integral) === false) {
                return $res->withStatus(400)->write('积分兑换格式错误');
                exit;
            }
            if ($this->checkfrom->regex('number', $num) === false) {
                return $res->withStatus(400)->write('库存格式错误');
                exit;
            }
            if ($this->checkfrom->regex('require', $rank) === false) {
                return $res->withStatus(400)->write('备注不能为空');
                exit;
            }

            $data = [
                'cur_id' => $this->account['id'],
                'clothing_name' => $clothing_name,
                'minage' => $minage,
                'maxage' => $maxage,
                'starttime' => strtotime($starttime),
                'endtime' => strtotime($endtime),
                'integral' => $integral,
                'num' => $num,
                'rank' => $rank,
                'tkcode'=>$tkcode,
                'content' => $this->remove_xss($content),
                'createtime' => time(),
                'updatetime' => time()

            ];
            if (!empty($newname)) {
                $data = array_merge($data, ['pic' => $newname]);
            }
            if (empty($id)) {
                $cid = $this->db->insertid('tcp_clothing', $data);
                $colorflag = 0;
                $sizeflag = 0;
                if (!empty($req->getParam('color'))) {
                    $colorarr = explode('|', $req->getParam('color'));

                    foreach ($colorarr as $cv) {
                        if ($this->db->insert('tcp_clothing_color', ['cid' => $cid, 'colorname' => $cv])) {
                            $colorflag++;
                            continue;
                        }
                    }

                }
                if (!empty($req->getParam('size'))) {
                    $sizerarr = explode('|', $req->getParam('size'));

                    foreach ($sizerarr as $sv) {
                        if ($this->db->insert('tcp_clothing_size', ['cid' => $cid, 'sizename' => $sv])) {
                            $sizeflag++;
                            continue;
                        }
                    }
                }
                if(empty($colorflag)){
                    return $res->withStatus(400)->write('请添加颜色');
                    exit;
                }
                if(empty($sizeflag)){
                    return $res->withStatus(400)->write('请添加尺寸');
                    exit;
                }


            } else {
                $result = $this->db->where(['id' => $id])->find('tcp_clothing');
                if (!empty($result)) {
                    unset($data['createtime']);
                    $cid = $this->db->where(['id' => $id])->update('tcp_clothing', $data);
                } else {
                    return $res->withStatus(400)->write('data error');
                    exit;
                }

            }
            if (!empty($cid)) {
                return $res->withStatus(200)->write('保存成功');
                exit;
            } else {
                return $res->withStatus(400)->write('保存失败');
                exit;
            }

        } else {
            return $res->withStatus(400)->write('guard error');
            exit;
        }


    }
    public function deleteclothing(Request $req, Response $res)
    {
        $id = $req->getParam('id');
        $arr=explode(',',$id);
        $error=0;
        foreach ($arr as $val){
            if($this->checkfrom->regex('number', (int)$val) === false){
                $error++;
                continue;
            }
        }
        if(empty($error)){
            $where['id']=array("(".$id.")",'in');
            $count = $this->db->where($where)->update('tcp_clothing', ['is_delete'=>1,'updatetime'=>time()]);
            if (!empty($count)) {
                return $res->withStatus(200)->write('删除成功');
                exit;
            } else {
                return $res->withStatus(400)->write('删除失败');
                exit;
            }
        }else{
            return $res->withStatus(400)->write('data error');
            exit;
        }




    }

}